﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using app_common;
using System.Collections;
using tpos_custom_control;

namespace tpos_front_end
{
    public partial class frm_front_end_table : Form
    {
        public frm_front_end_table()
        {
            InitializeComponent();
        }

        private void frm_front_end_main_Load(object sender, EventArgs e)
        {
            Hashtable hashTpos = TPOS.ReadPosConfig();
            if (hashTpos[STATUS_MESSAGE.MESSAGE].ToString() == STATUS_MESSAGE.ERROR)
            {
                MessageBox.Show(hashTpos[STATUS_MESSAGE.VALUE].ToString(), STATUS_MESSAGE.CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                APP_FORM.frm_current.Close();
                APP_FORM.frm_parent.Show();
            }
            else { 

                //Get table list base on region code
                string region_code = TPOS.hashConfig["region_code"].ToString();
                string sql = "SELECT \"code\", \"name\", \"region_code\", \"region_name\", \"enable\", \"open\" " +
                            "FROM \"table\" " +
                            "WHERE \"region_code\" = '" + region_code + "' AND \"enable\" = '" + DEFINE_VALUE.YES + "' " +
                            "ORDER BY \"name\"";

                Hashtable hashResult = APP_SQL_COMMAND.ExecuteReader(sql);

                if (hashResult[STATUS_MESSAGE.MESSAGE].ToString() == STATUS_MESSAGE.ERROR)
                {
                    MessageBox.Show("Không thể liệt kê danh sách bàn.\n\rChi tiết: " + hashResult[STATUS_MESSAGE.VALUE].ToString(), STATUS_MESSAGE.CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    APP_FORM.frm_current.Close();
                    APP_FORM.frm_parent.Show();
                }
                else {
                    DataTable dt = (DataTable)hashResult[STATUS_MESSAGE.VALUE];
                    foreach (DataRow dr in dt.Rows) {
                        uc_table uc_table = new uc_table();
                        uc_table.Name = dr["name"].ToString();
                        uc_table.Tag = dr["code"].ToString();
                        flp_layout.Controls.Add(uc_table);
                        if (dr["open"].ToString() == DEFINE_VALUE.YES) {
                            uc_table.uc_Open = true;
                        }
                        uc_table.uc_Text = dr["name"].ToString();
                    }
                }
            }

            APP_DELEGATE.TABLE_CLICK = new APP_DELEGATE.UC_TABLE_CLICK(TableClickFunction);
            txt_add_table.uc_Forcus = true;

            APP_DELEGATE.TEXTBOX_KEYUP = new APP_DELEGATE.UC_TEXTBOX_KEYUP(UcTextBoxKeyUpProcess_KeyUp);
        }
        #region "Delegate details..."

        private void TableClickFunction(string code, string name)
        {
            ListViewItem lvit = new ListViewItem(name);
            lvit.Tag = code;
            lv_table_code_list.Items.Add(lvit);
            txt_add_table.uc_Forcus = true;
        }

        private void UcTextBoxKeyUpProcess_KeyUp(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Enter) {
                e.Handled = true;
                btn_add_table_ok_Click(sender,e);
            }
        }

        #endregion
        private void btn_close_Click(object sender, EventArgs e)
        {
            APP_FORM.frm_current.Close();
            APP_FORM.frm_parent.Show();
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            string cancel_list = "";
            foreach (ListViewItem lvit in lv_table_code_list.Items) {
                cancel_list += lvit.Tag + ";";
            }

            foreach (Control ctrl in flp_layout.Controls) { 
                if(ctrl is uc_table){
                    if (cancel_list.IndexOf(ctrl.Name) >= 0) {
                        ((uc_table)ctrl).uc_Enable = true;
                    }
                }
            }

            lv_table_code_list.Items.Clear();
        }

        private void btn_order_Click(object sender, EventArgs e)
        {
            string table_code_list = "";
            foreach (ListViewItem lvit in lv_table_code_list.Items) {
                table_code_list += lvit.Tag + ";";
            }
            if (table_code_list.Length > 0)
            {
                table_code_list = table_code_list.Substring(0, table_code_list.Length - 1);
            }
            MessageBox.Show(table_code_list);
        }

        private void btn_add_table_ok_Click(object sender, EventArgs e)
        {
            
            foreach (Control ctrl in flp_layout.Controls) {
                if (ctrl is uc_table) {
                    if (ctrl.Tag.ToString().Equals(txt_add_table.uc_Text.Trim())) {
                        ((uc_table)ctrl).uc_Click(sender,e);
                        txt_add_table.uc_Forcus = true;
                        break;
                    }
                }
            }

            txt_add_table.uc_Text = "";

        }

        private void frm_front_end_table_KeyUp(object sender, KeyEventArgs e)
        {
            switch(e.KeyCode){
                case Keys.F9: btn_clear_Click(sender,e); break;
                case Keys.F10: btn_add_table_ok_Click(sender,e); break;
                case Keys.F11: btn_order_Click(sender, e); break;
                case Keys.F12: btn_close_Click(sender,e); break;
            }
        }
    }
}
